<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/5/10
 * Time: 21:46
 */

namespace app\admin\controller;

use app\admin\model\AdminList;
use app\admin\model\AuthGroup;
use app\admin\model\AuthGroupAccess;
use app\admin\model\AuthRule;
use app\admin\model\OrderList;
use think\Controller;
use think\facade\Session;

class Index extends Base
{
    public function index(){
        $admin_id = Session::get('admin_id');
        $admin = AuthGroupAccess::where('uid',$admin_id)->field('uid,group_id')->find();
        $role  = AuthGroup::where('id',$admin['group_id'])->where('status',1)->find();
        $role['rule'] = explode(',',$role['rules']);
        $rule = AuthRule::where('id','in',$role['rule'])
            ->where('types',1)->order('sort desc')->where('status',1)->select()->toArray();
        $ret = $this->roleArray($rule);
        $user=AdminList::where('id',$admin_id)->find();
        $this->assign('admin',$user);
        $this->assign('menu',$ret);
        return $this->fetch();
    }
    public function shopImage(){
        $file =request()->file('file');
        $ret = $this->upload($file);
        return json(['code'=>1,'url'=>$ret]);
    }
    public function welcome(){
        $admin_id = Session::get('admin_id');
        $user=AdminList::where('id',$admin_id)->find();
        $this->assign('admin',$user);
        return $this->fetch();
    }

    public function dataStats(){
        $time = input('post.time')?input('post.time'):date('Y-m-d',time());
        $type = input('post.type')?input('post.type'):1;
        $member = [];
        $money = [];
        $times = [];
        $end_time = strtotime($time);
        if ($type == 1){
            for ($i=0,$item=12;$i<$item;$i++){
                $start_time= $end_time;
                $end_time += 3600 * 2;
                $member[$i] = OrderList::where('create_time','>=',$start_time)
                    ->where('create_time','<',$end_time)->where('order_status',1)->count();
                $price = OrderList::where('create_time','>=',$start_time)
                    ->where('create_time','<',$end_time)->where('order_status',1)->column('pay_money');
                $money[$i] = array_sum($price);
            }
            $X_axis = ['2点','4点','6点','8点','10点','12点','14点','16点','18点','20点','22点','24点'];
        }elseif($type == 2){
            $end_time = strtotime($this->getWeekRange($time,1));
            for ($i=0,$item=7;$i<$item;$i++){
                $start_time= $end_time;
                $end_time += 3600 * 24;
                $member[$i] = OrderList::where('create_time','>=',$start_time)
                    ->where('create_time','<',$end_time)->where('order_status',1)->count();
                $price = OrderList::where('create_time','>=',$start_time)
                    ->where('create_time','<',$end_time)->where('order_status',1)->column('pay_money');
                $money[$i] = array_sum($price);
                $times[$i]['start'] = date('Y-m-d',(int)$start_time);
                $times[$i]['end'] = date('Y-m-d',(int)$end_time);
            }
            $X_axis = ['周一','周二','周三','周四','周五','周六','周日'];
        }else{
            $firstday = date('Y-m-01', strtotime($time));
            $lastday = date('Y-m-d', strtotime("$firstday +1 month -1 second"));
            $end_time = strtotime($firstday);
            $X_axis = [];
            for ($i=0,$item=6;$i<$item;$i++){
                $start_time = $end_time;
                $end_time += 3600 * 24 * 4;
                if ($i == 5){
                    $end_time = strtotime($lastday);
                }
                $member[$i] = OrderList::where('create_time','>=',$start_time)
                    ->where('create_time','<=',$end_time)->where('order_status',1)->count();
                $price = OrderList::where('create_time','>=',$start_time)
                    ->where('create_time','<=',$end_time)->where('order_status',1)->column('pay_money');
                $money[$i] = array_sum($price);
                $X_axis[$i] = date('Y-m-d',(int)$end_time);
            }
        }
        return json(['code'=>1,'msg'=>'返回成功','X_axis'=>$X_axis,'member'=>$member,'money'=>$money,'start'=>$times]);
    }

    private function getWeekRange($date, $start=0){
        // 将日期转时间戳
        $dt = new \DateTime($date);
        $timestamp = $dt->format('U');
        // 获取日期是周几
        $day = (new \DateTime('@'.$timestamp))->format('w');
        // 计算开始日期
        if($day>=$start){
            $startdate_timestamp = mktime(0,0,0,date('m',$timestamp),date('d',$timestamp)-($day-$start),date('Y',$timestamp));
        }elseif($day<$start){
            $startdate_timestamp = mktime(0,0,0,date('m',$timestamp),date('d',$timestamp)-7+$start-$day,date('Y',$timestamp));
        }
        // 结束日期=开始日期+6
        $enddate_timestamp = mktime(0,0,0,date('m',$startdate_timestamp),date('d',$startdate_timestamp)+6,date('Y',$startdate_timestamp));
        $startdate = (new \DateTime('@'.$startdate_timestamp))->format('Y-m-d');
//        $enddate = (new \DateTime('@'.$enddate_timestamp))->format('Y-m-d');
        return $startdate;
    }

}